package com.example.mybatisplus.video.service;

import com.example.mybatisplus.video.domain.Video;
import com.example.mybatisplus.video.mapper.VideoMapper;
import com.example.mybatisplus.video.util.IDUtil;
import com.example.mybatisplus.video.util.ResultObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhouxl
 * @DATE 2020/9/27
 * @descriptoin
 */
@Component
public class VideoSerivce {

    @Autowired
    private VideoMapper videoMapper;

    /**
     * 上传视频处理
     *
     * @param file
     * @return
     */
    public Map<String, Object> upload(MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        Video video = new Video();
        String fileName = file.getOriginalFilename();
        byte[] bs = new byte[0];
        try {
            bs = file.getBytes();
        } catch (IOException e) {
            result.put("code", "400");
            result.put("msg", "视频解析异常");
        }
        video.setId(IDUtil.uuid());
        video.setBs(bs);
        video.setName(fileName);
        int count = videoMapper.addVideo(video);
        if (count != 0) {
            result.put("code", "200");
            result.put("msg", "视频上传成功");
        } else {
            result.put("code", "400");
            result.put("msg", "视频上传失败");
        }
        return result;
    }

    /**
     * 通过视频标识查询视频信息
     *
     * @param vid
     * @return
     */
    public Map<String, Object> queryVideo(String vid) {
        Map<String, Object> success = ResultObjUtil.success();
        if (Objects.isNull(vid)) {
            success.put("code", "400");
            success.put("msg", "参数异常");
            return success;
        }
        Video video = videoMapper.queryVideo(vid);
        if (Objects.isNull(video)) {
            success.put("code", "400");
            success.put("msg", "视频数据不存在");
            return success;
        }
        success.put("resultData", video);
        return success;
    }
}
